package Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.DAOException;
import DAO.DAOFactory;
import DAO.OTClaimDAO;
import DAO.MySQL.OTClaimDAOImpl;
import Model.OTClaimBean;

/**
 * Servlet implementation class OTClaimController
 */
@WebServlet("/OTClaimController")
public class OTClaimController extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OTClaimController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub	
		//processRequest(request,response);
	}
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
		
//		PrintWriter out = response.getWriter();		
//		out.println("<html><body>" + IorU + " &&& " +  + "</body></html>");		
		
		String isC_R_U_D = request.getParameter("isC_R_U_D");
			
		OTClaimDAO OTClaimDaoObj = DAOFactory.getInstance().getOTClaimDAO();		
		OTClaimBean OTClaimObj = new OTClaimBean();		
		
		OTClaimObj.setEmployeeId(2);  // replace with session value
		
		if(!isC_R_U_D.equalsIgnoreCase("R"))
		{
			OTClaimObj.setOtDate(request.getParameter("oTDate"));
			OTClaimObj.setOtHr(Integer.parseInt(request.getParameter("oTHr")));
			OTClaimObj.setProjectName(request.getParameter("projectName"));		
		}			
		
		if(isC_R_U_D.equalsIgnoreCase("C"))
		{
			try {
				OTClaimDaoObj.insertOTClaim(OTClaimObj);			
			} catch (Exception e1) {			
				System.out.println("Exception occured");
				e1.printStackTrace();
			}
		}
		else if(isC_R_U_D.equalsIgnoreCase("U"))
		{
			OTClaimObj.setClaimId(Integer.parseInt(request.getParameter("claimId")));
		
			try {
				OTClaimDaoObj.updateOTClaim(OTClaimObj);			
			} catch (Exception e1) {			
				System.out.println("Exception occured");
				e1.printStackTrace();
			}
		}		
		
		ArrayList<OTClaimBean> OTList = new ArrayList<OTClaimBean>();
		try {
			OTList = OTClaimDaoObj.findEmployee(OTClaimObj.getEmployeeId());  // replace with session value
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		HttpSession session=request.getSession(true);
		//session.setAttribute("OTClaimSs", OTClaimObj);
		session.setAttribute("OTList", OTList);
		
		RequestDispatcher rd = request.getRequestDispatcher("/OTClaimListPage.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 		
	}
}
